﻿(function ($) {
    var search_page = {
        history: [],
        change_history: false,
        on_slide: false
    }
    search_page.hide_page = function () {
        $('#search_defer').removeClass('hide');
        $('#search_page').removeClass('show').addClass('hide');
        $('#search_back').show();
        $('#search_cancel').hide();
        $('.search_input').val('');
        $('.search_links').show();
    }
    search_page.init = function () {
        var self = this;
        $('#search_defer').click(function () {
            if (this.offsetLeft != 0) {
                search_page.hide_page();
                return false;
            }
        });
        $('.search_links').height($('#search_page').height() - 46);
        $('#search_back').click(function () {
            search_page.hide_page();
            return false;
        });
        $('.search_input').focus(function () {
            if ($(this).val().length == 0) {
                $('.search_links').hide();
            }
            $('#search_page').addClass('mach-width');
            $('#history_back').show();
            $('#search_cancel').show();
            $('#search_back').hide();
        });
        $('#history_back').click(function () {
            $('#search_back').show();
            $('#search_cancel').hide();
            $('.search_input').val('');
            $('.search_links').show();
            $('#search_page').removeClass('mach-width');
            $(this).hide();
        })
        $('#search_cancel').click(function () {
            var val = $('.search_input').val().replace(/(^\s+)|(\s+$)/g, "");
            if (val == "") {
                return null;
            }
            else {
                window.location.href = "result.html?key=" + encodeURIComponent($('.search_input').val());
            }
        });
        document.onkeydown = function () {
            if (event.keyCode == 13) {
                //$('#search_cancel').trigger('click');
                alert(encodeURIComponent($('.search_input').val()));
                window.location.href = "result.html?key=" + encodeURIComponent($('.search_input').val());
            }
        }
    }
    window.search_page = search_page;
    search_page.init();
})(window.jQuery || window.Zepto)